package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"mysql-read-excel/excel"
	"mysql-read-excel/model"
	"net/http"
)

func main() {

	AbolishOperatorIntoExcel()

}

var abolishOperator []*model.Tuple5

// AbolishOperatorIntoExcel 将excel读取后, 加工, 再写入excel
func AbolishOperatorIntoExcel() {

	fileName := "file/abolish_operator.xlsx"
	sheetName := "Sheet1"
	rows, err := excel.GetRows(fileName, sheetName)
	if err != nil {
		fmt.Println(err)
		return
	}

	for idx, row := range rows {
		fmt.Println("idx=", idx, "len=", len(abolishOperator))
		length := len(row)
		checkMember(row, length)
	}

	var twoDimensionArray [][]interface{}
	for _, tuple5 := range abolishOperator {
		twoDimensionArray = append(twoDimensionArray, []interface{}{tuple5.A, tuple5.B, tuple5.C, tuple5.D, tuple5.E})
	}
	fmt.Println(twoDimensionArray)
	excel.CreateMultipleXlsx(map[string][][]interface{}{
		"Sheet1": twoDimensionArray,
	}, "file/abolish_operator2")
}

func checkMember(row []string, length int) {
	abolishOperator = append(abolishOperator, &model.Tuple5{
		A: getRowVal(row, length, 0),
		B: getRowVal(row, length, 1),
		C: getRowVal(row, length, 2),
		D: getRowVal(row, length, 3),
		E: getRowVal(row, length, 4),
	})
}

func getRowVal(row []string, length, idx int) string {
	if length > idx {
		return memberExists(row[idx])
	}
	return ""
}

func memberExists(memberName string) string {
	if memberName == "-" || memberName == "" {
		return "-"
	}
	statusId, err := PostJSON("http://11.134.48.49/tof/api/GetStaffInfo",
		map[string]map[string]string{
			"params": {
				"user_name": memberName,
			},
		})
	if err != nil {
		fmt.Println("memberExists error", err.Error())
		return memberName
	}
	if statusId != "1" {
		return "离职"
	}
	return memberName
}

//PostJSON post json 数据请求
func PostJSON(uri string, obj interface{}) (string, error) {
	jsonData := make([]byte, 0, 0)
	if str, ok := obj.(string); ok {
		jsonData = []byte(str)
	} else {
		var err error
		jsonData, err = json.Marshal(obj)
		if err != nil {
			return "error", err
		}
	}

	jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<"))
	jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">"))
	jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&"))
	body := bytes.NewBuffer(jsonData)
	response, err := http.Post(uri, "application/json;charset=utf-8", body)
	if err != nil {
		return "error", err
	}
	defer response.Body.Close()

	if response.StatusCode != http.StatusOK {
		return "error", fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
	}

	rsp, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return "error", err
	}
	bodyMap := map[string]interface{}{}
	err = json.Unmarshal(rsp, &bodyMap)
	//fmt.Println(bodyMap)
	resultInfo := bodyMap["result"]
	if resultInfo == nil {
		return "error", errors.New("nil")
	}
	resultMap := resultInfo.(map[string]interface{})
	if resultMap != nil {
		//fmt.Println(resultMap["StatusId"])
		statusId := int(resultMap["StatusId"].(float64))
		return fmt.Sprintf("%d", statusId), nil
	}
	return "1", nil
}
